package com.yanqu.road.server.cmd.banquet;

import com.yanqu.road.entity.banquet.BanquetGiftInfo;
import com.yanqu.road.entity.banquet.cross.CrossBanquetData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.server.manager.banquet.CrossBanquetConfigMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.banquet.CrossBanquetMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;


@Cmd(code = Protocol.C_CROSS_OPEN_BANQUET, desc = "开启跨服宴会")
public class OpenBanquetCmd extends CrossCommand {


    private long userId;
    private String banquetId;
    private boolean autoOpen;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CrossBanquetProto.OpenCrossBanquetReq reqMsg = CrossBanquetProto.OpenCrossBanquetReq.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        banquetId = reqMsg.getBanquetId();
        if (reqMsg.hasAutoOpen()) {
            autoOpen = reqMsg.getAutoOpen();
        }

        int ret = CrossBanquetMgr.openBanquet(banquetId);
        CrossBanquetProto.OpenCrossBanquetResp.Builder respMsg = CrossBanquetProto.OpenCrossBanquetResp.newBuilder();
        respMsg.setAutoOpen(autoOpen);
        if (ret == 0) {
            CrossBanquetData banquetData = CrossBanquetMgr.getBanquet(banquetId);
            respMsg.setBanquetId(banquetId);
            respMsg.setOpenTime(banquetData.getOpenTime());
            respMsg.setOverTime(banquetData.getOverTime());

//            //自己加入宴会
//            if (CrossBanquetConfigMgr.canJoinSelf(banquetData.getType()) && reqMsg.hasPlayerBaseData()) {
//                UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
//                BanquetGiftInfo banquetGiftInfo = CrossBanquetConfigMgr.getBanquetGiftInfoByBanquetInfoId(banquetData.getType());
//                if (banquetGiftInfo != null) {
//                    CrossBanquetMgr.joinBanquet((int) serverId, userId, banquetId, banquetGiftInfo.getId(), reqMsg.getTitleAdd(), reqMsg.getPatronsSkillAdd(), userBaseInfo,
//                            0, 0, 0, 0);
//                    respMsg.setGiftId(banquetGiftInfo.getId());
//                }
//            }
        }
        respMsg.setRet(ret);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_BANQUET_CROSS_OPEN_RESP, respMsg);
        MessageHelper.sendPacket(serverId, userId, pbMsg);

        //放到后面，先同步給自己后同步給各個屈服
        CrossBanquetMgr.syncBanquetServer(banquetId);
    }

    @Override
    public void sendData() {

    }
}
